import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../r.dart';

/**
 * @Description: 首页标题栏
 * @Author: 下雨了
 * @CreateDate: 2022-04-01 9:42
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-6-14 10:19:48
 * @UpdateRemark: 1、添加背景图  预留渐变
 *                2、返回icon做区别
 *                3、预留高度设置
 * @Version: 1.1
 */
///
PreferredSizeWidget MyAppBar(
  BuildContext context, {
  double? elevation = 0, // 阴影 默认高度
  Color? backgroundColor = Colors.transparent, // 默认背景颜色
  IconData? iconData, // 标题图标
  String? title, // 标题
  Color? titleColor = Colors.white, // 标题颜色
  double? fontSize, // 标题字体大小
  FontWeight? fontWeight = FontWeight.w600, // 标题字体粗细
  bool isShowLeading = true, // 是否显示左边按钮 默认显示
  Widget? leading, // 左侧返回
  Color? leadingColor = Colors.white, // 左侧按钮颜色
  VoidCallback? backTap, // 左侧还回按钮监听
  List<Widget>? actions, // 右侧
  PreferredSizeWidget? bottom, // 底部
  Widget? flexibleSpace, // 弹性空间，背景图片
}) {
  return
    // PreferredSize(
    // preferredSize: Size.fromHeight(45.5.r), //设置高度
    // child:
  AppBar(
      elevation: elevation,
      centerTitle: true,
      backgroundColor: backgroundColor,
      title: CentreText(iconData, title, titleColor, fontSize, fontWeight),
      automaticallyImplyLeading: isShowLeading,
      leading: isShowLeading == true ? leading ?? DefaultBack(context, leadingColor: leadingColor, callback: backTap) : null,
      actions: actions,
      bottom: bottom,
      flexibleSpace: flexibleSpace ?? Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(R.assetsImgBg21), fit: BoxFit.fill),
        // gradient: LinearGradient(colors: [
        //     Color(0xFF0018EB),
        //     Color(0xFF01C1D9),
        //   ], begin: Alignment.bottomCenter, end: Alignment.topCenter),
        ),
      ),
    // ),
  );
}

/// 中间文字
Widget CentreText(
  IconData? iconData, // 标题图标
  String? title,
  Color? titleColor, // 标题颜色
  double? fontSize, // 标题字体大小
  FontWeight? fontWeight, //  标题字体粗细
) {
  return iconData == null
      ? Text(
          title ?? "",
          style: TextStyle(color: titleColor ?? Colors.white, fontSize: fontSize ?? 16.sp, fontWeight: fontWeight),
        )
      : RichText(
          text: TextSpan(children: [
            WidgetSpan(
              child: Icon(
                iconData,
                color: Colors.white,
                size: 18.r,
              ),
            ),
            TextSpan(
              text: title ?? "",
              style: TextStyle(color: titleColor ?? Colors.white, fontSize: fontSize ?? 16.sp, fontWeight: fontWeight),
            ),
          ]),
        );
}

/// 默认返回按钮  参考 BackButtonIcon
Widget DefaultBack(
  BuildContext context, {
  Color? leadingColor = Colors.white,
  VoidCallback? callback,
}) {
  return IconButton(
    onPressed: () {
      if (callback != null) {
        callback();
      } else {
        Navigator.maybePop(context);
      }
    },
    color: leadingColor,
    tooltip: MaterialLocalizations.of(context).backButtonTooltip,
    icon: Icon(
      // Platform.isIOS ? Icons.arrow_back_ios_outlined : Icons.arrow_back_outlined,
      Icons.arrow_back_ios_outlined,
      size: 20.r,
    ),
  );
}
